package com.hfi.ood.litejunit.v2.framework.decorator;

import com.hfi.ood.litejunit.v2.framework.Test;
import com.hfi.ood.litejunit.v2.framework.TestResult;

import java.util.stream.IntStream;

/**
 * @author ChangLiang
 * @date 2020/5/27
 */
public class RepeatedTest extends TestDecorator {

    private int fTimesRepeat;

    public RepeatedTest(Test test, int fTimesRepeat) {
        super(test);
        if (fTimesRepeat < 0) {
            throw new IllegalArgumentException("Repetition count must be > 0");
        }
        this.fTimesRepeat = fTimesRepeat;
    }

    @Override
    public int countTestCases() {
        return super.countTestCases() * fTimesRepeat;
    }

    @Override
    public void run(TestResult testResult) {
        IntStream.range(0,fTimesRepeat).forEach(i->{
            if (testResult.shouldStop()) {
                return;
            }
            super.run(testResult);
        });
    }

    @Override
    public String toString() {
        return "RepeatedTest{" +
                "fTimesRepeat=" + fTimesRepeat +
                '}';
    }
}
